<?php
include_once('../../php/postgre.php');
$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];
if($act=='save'){
	$id_bidang = $_POST['id_bidang'];
	$nama_bidang = $_POST['text-bidang'];
	
	if(!is_numeric($id_bidang)){
		echo "Err. invalid id.";
		exit;
	}
	
	if($id_bidang > 0){
		exec_query("update spm_bidang_urusan set nama_bidang='".$nama_bidang."' WHERE id_bidang = ".$id_bidang);
		echo 'success__';
	}else{
		$maxid = get_maxid('id_bidang','spm_bidang_urusan');
		exec_query("insert into spm_bidang_urusan(id_bidang, nama_bidang) values ('".$maxid."','".$nama_bidang."')");
		$stored = get_data('select id_bidang from spm_bidang_urusan where id_bidang='.$maxid);
		if($stored['id_bidang']==$maxid){
			echo 'success__';
			$id_bidang = $maxid;
		}
	}
	
	echo $id_bidang.'__';
	view_program();
}else if($act == 'delete'){
	$id_bidang = $_POST['id_bidang'];
	if(is_numeric($id_bidang)){
		exec_query("DELETE FROM spm_bidang_urusan WHERE id_bidang =".$id_bidang);
		echo 'success__';
	}else{
		echo 'Error';
	}
	
	echo $id_bidang.'__';
	view_program();
}else if($act=='edit'){
	$id_bidang = $_POST['id_bidang'];
	if(is_numeric($id_bidang)){
		$data = get_data("select id_bidang, nama_bidang from spm_bidang_urusan where id_bidang=".$id_bidang);
		$ress = implode($data,'__');
		print_r($ress);
	}else{
		echo $id_bidang;
		echo 'errori';
	}
}

function view_program(){
	$no = 1;
	$data = get_datas("SELECT * FROM spm_bidang_urusan");
	foreach($data as $data){
	?>
	<tr>
		<td style="text-align:center;"><?php echo $no ?></td>
		<td><?php echo $data['nama_bidang'] ?></td>
		<td style="width:200px; text-align:center;">	<a class="btn btn-info btn-edit" name="<?php echo $data['id_bidang']?>"><i class="icon-edit icon-white"></i>&nbsp; Edit</a>
														<a class="btn btn-danger btn-delete" name="<?php echo $data['id_bidang']?>"><i class="icon-trash icon-white"></i>&nbsp; Delete</a>
		</td>
	</tr>
	<?php
	$no++;
	}
}
	
?>